// login module
import { makeAutoObservable } from 'mobx'
import { http, setToken, getToken, removeToken, setStorage, getStorage } from '@/utils'
class LoginStore {
  token = getToken() || ''
  userId = getStorage('userid') || ''
  userName = getStorage('username') || ''
  constructor() {
    // 响应式
    makeAutoObservable(this)
  }
  getToken = async ({ username, password, captcha, uuid }) => {
    // 调用登录接口
    const res = await http.post('/invest/sys/login', {
      username, password, captcha, uuid
    })
    if (res.code === 0) {
      // 存入token
      this.token = res.token
      this.userId = res.body.userId
      this.userName = res.body.username
      // 存入ls
      setToken(this.token)
      setStorage('userid', this.userId)
      setStorage('username', this.userName)
    }
    return res
  }
  // 退出登录
  loginOut = () => {
    this.token = ''
    removeToken()
  }
}


export default LoginStore